package util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

public class GameUtil {

    public static Image getImage(String imagePath){
        URL url = GameUtil.class.getClassLoader().getResource(imagePath);
        BufferedImage image = null;
        try {
            image = ImageIO.read(url);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("没有找到图片");
        }
        return image;
    }

    public static Image rotateImage(final BufferedImage bufferedImage, final int degree) {
        int w = bufferedImage.getWidth();
        int h = bufferedImage.getHeight();
        int type = bufferedImage.getColorModel().getTransferType();

        BufferedImage image;// Empty image.
        Graphics2D graphics2d; // Empty bush.

        (graphics2d = (image = new BufferedImage(w, h, type)).createGraphics())
                .setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.rotate(Math.toRadians(degree),w/2,h/2);
        graphics2d.drawImage(bufferedImage,0,0,null);
        graphics2d.dispose();
        return image;
    }
}
